package com.roberto.main;

import com.badlogic.gdx.Gdx;

public class Preferences {
    public boolean showFPS;
    private com.badlogic.gdx.Preferences cfg = Gdx.app.getPreferences("cfg.xml");
    private int width, height;

    public Preferences() {
        if (!cfg.contains("WIDTH")) {
            cfg.clear();
            cfg.putInteger("WIDTH", 1024);
            cfg.putInteger("HEIGHT", 768);
            cfg.putBoolean("Fullscreen", false);
            cfg.putBoolean("Vsync", true);
            cfg.putBoolean("ShowFPS", false);
            cfg.putInteger("RECORD", 0);
            cfg.flush();
        }
        width = cfg.getInteger("WIDTH");
        height = cfg.getInteger("HEIGHT");
        showFPS = cfg.getBoolean("ShowFPS");
    }

    public void setHeight(int height) {
        cfg.putInteger("HEIGHT", height);
        this.height = height;
        cfg.flush();
    }

    public void setWidth(int width) {
        cfg.putInteger("WIDTH", width);
        this.width = width;
        cfg.flush();
    }

    public void setFullScreen(boolean fs) {
        cfg.putBoolean("Fullscreen", fs);
        cfg.flush();
    }

    public void setVsync(boolean vsync) {
        cfg.putBoolean("Vsync", vsync);
        cfg.flush();
    }

    public void setScore(int score) {
        if (getRecord() < score) {
            cfg.putInteger("RECORD", score);
            cfg.flush();
        }
    }

    public void setShowFps(boolean fps) {
        cfg.putBoolean("ShowFPS", fps);
        cfg.flush();
        showFPS = fps;
    }

    public void updateCFG() {
        Gdx.graphics.setDisplayMode(width, height, isFullScreen());
        Gdx.graphics.setVSync(getVsync());

    }

    public void setSelection(int select) {
        switch (select) {
        case 0:
            cfg.putInteger("WIDTH", 800);
            cfg.putInteger("HEIGHT", 600);
            break;
        default:
        case 1:
            cfg.putInteger("WIDTH", 1024);
            cfg.putInteger("HEIGHT", 768);
            break;
        case 2:
            cfg.putInteger("WIDTH", 1280);
            cfg.putInteger("HEIGHT", 1024);
            break;
        case 3:
            cfg.putInteger("WIDTH", 1366);
            cfg.putInteger("HEIGHT", 768);
            break;
        case 4:
            cfg.putInteger("WIDTH", 1680);
            cfg.putInteger("HEIGHT", 1050);
            break;

        }
        width = cfg.getInteger("WIDTH");
        height = cfg.getInteger("HEIGHT");
        cfg.flush();
    }

    public boolean getVsync() {
        return cfg.getBoolean("Vsync");
    }

    public int getRecord() {
        return cfg.getInteger("RECORD");
    }

    public boolean isFullScreen() {
        return cfg.getBoolean("Fullscreen");
    }

    public String getSelection() {
        return width + "x" + height;
    }

}
